/*********************************************************************
Replication Materials for "Explanation Giving Promotes Democratic Satisfaction Regardless of Respondent Partisanship"

Syntax produces the single-explanation IV models discussed in Online Appendix A
(Tables OA4-OA6). 

Syntax also saves the output that will be plotted in Figures OA4 & OA5 (via an R syntax file)
***********************************************************************/

/************
Working Directory and Data
Update working directory to where you have the data
********************/

cd "C:\Users\jar68\OneDrive\Ongoing Work\Explanations and Dem Support\P. Behavior\Revision\R2\RR3\FINAL\Dataverse\"
use "ess_final.dta"

**Dropping non-democracies; Bulgaria had caretaker gov't
drop if country == "Albania" | country == "Russia" | country == "Kosovo" | country == "Ukraine" | country == "Bulgaria"

/*********************************
Weights
*********************************/

gen anweight = pspwght*pweight
label var anweight "Analysis Weight"


/*********************************
Models
*********************************/

*********Just V-Dem********
eststo clear
local conts " std_courts std_elections std_econ std_life std_socialtrust i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen i.east_europe c.enep_comb c.unemp_ch"
foreach var in gov_satis dem_satis {
	eststo: mixed `var' reason_std [pweight=anweight] || country:  
			margins, at(reason_std=(-2(1)2)) saving(`var'_reason_pred_justvdem_m1, replace)
	eststo: mixed `var' reason_std i.co_partisan `conts'  [pweight=anweight] || country: 
			margins, at(reason_std=(-2(1)2)) saving(`var'_reason_pred_justvdem_m2, replace)
	eststo: mixed `var' c.reason_std##i.co_partisan `conts' [pweight=anweight] || country: 
			*reasons * co-partisanship
				margins, dydx(reason_std) by(co_partisan) saving(`var'_reason_dydx_justvdem_m3, replace)
				margins, dydx(co_partisan) at(reason_std=(-2(1)2)) saving(`var'_cop_reason_dydx_justvdem_m3, replace)
				margins co_partisan, at(reason_std=(-2(1)2)) saving(`var'_reason_pred_justvdem_m3, replace)
}

***Full Table
esttab using table_oa5.rtf, label onecell replace se b(3) aic bic nobaselevels ///
		transform(ln*: exp(2*@) 2*exp(2*@)) ///
		eqlabels("" "var(Constant)" "var(Residual)", none) ///
		mtitles("Gov" "Gov" "Gov" "Dem" "Dem" "Dem") ///
		title("{\b Table OA5}: Explanation Giving and Political Support (V-Dem Only)") ///
		addnotes("DV ranges from 0-10. All continuous variables are country-standardized save for age in years. Analyses use survey weights.")


*********Just ESS********
eststo clear
local conts " std_courts std_elections std_econ std_life std_socialtrust i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen i.east_europe c.enep_comb c.unemp_ch"
foreach var in gov_satis dem_satis {
	eststo: mixed `var' std_explains  [pweight=anweight] || country:  
			margins, at(std_explains=(-2(1)2)) saving(`var'_explain_pred_justess_m1, replace)
	eststo: mixed `var' std_explains i.co_partisan `conts'  [pweight=anweight] || country: 
			margins, at(std_explains=(-2(1)2)) saving(`var'_explain_pred_justess_m2, replace)
	eststo: mixed `var' c.std_explains##i.co_partisan `conts' [pweight=anweight] || country: 
			*explains * co-partisanship
				margins, dydx(std_explains) by(co_partisan) saving(`var'_explain_dydx_justess_m3, replace)
				margins, dydx(co_partisan) at(std_explains=(-2(1)2)) saving(`var'_cop_explain_dydx_justess_m3, replace)
				margins co_partisan, at(std_explains=(-2(1)2)) saving(`var'_explain_pred_justess_m3, replace)
			
}

***Full Table
esttab using table_oa6.rtf, label onecell replace se b(3) aic bic nobaselevels ///
		transform(ln*: exp(2*@) 2*exp(2*@)) ///
		eqlabels("" "var(Constant)" "var(Residual)", none) ///
		mtitles("Gov" "Gov" "Gov" "Dem" "Dem" "Dem") ///
		title("{\b Table OA6}: Explanation Giving and Political Support (Just ESS)") ///
		addnotes("DV ranges from 0-10. All continuous variables are country-standardized save for age in years. Analyses use survey weights.")


		